package com.example.dailyrecord.service.impl;

import com.example.dailyrecord.base.entity.SQLBuilder;
import com.example.dailyrecord.base.service.impl.BaseServiceImpl;
import com.example.dailyrecord.entity.StatusCode;
import com.example.dailyrecord.entity.Subject;
import com.example.dailyrecord.service.SubjectService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class SubjectServiceImpl extends BaseServiceImpl<Subject> implements SubjectService {

    @Override
    public StatusCode insertRequest(HttpServletRequest request, HttpServletResponse response) {
        String km = request.getParameter(Subject.KM_NAME);
        if (StringUtils.isEmpty(km)){
            return StatusCode.FAIL;
        }
        String email = getEmail(request);
        Subject subject = Subject.getInstance(km, email);
        insert(subject);
        return StatusCode.SUCCESS;
    }

    @Override
    public PageInfo<Subject> getListRequest(HttpServletRequest request, HttpServletResponse response) {
        String email = getEmail(request);
        return PageInfo.of(selectList(SQLBuilder.getInstance().eq(Subject.UID_COLUMN, email)));
    }
}
